package sda.app_01.fragment;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;

import sda.app_01.LruImageCache;
import sda.app_01.R;

/**
 * Created by scorpio on 15-3-18.
 */
public class fragment_gps extends Fragment {

    @ViewInject(id = R.id.textView2)
    TextView tv_title ;

    @ViewInject(id = R.id.image)
    NetworkImageView networkImageView ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_gps,null);
        FinalActivity.initInjectedView(this,view);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        RequestQueue queue =  Volley.newRequestQueue(getActivity());
        LruImageCache lruImageCache =LruImageCache.instance();
        ImageLoader loader = new ImageLoader(queue,lruImageCache);
        networkImageView.setImageUrl("http://aircallin.u.qiniudn.com/device-2014-09-11-113906.png",loader);

        setUp();
    }

    private void setUp() {
        LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
        final Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                2000, 8, new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        update(location);
                    }

                    @Override
                    public void onStatusChanged(String provider, int status, Bundle extras) {
                        update(location);
                    }

                    @Override
                    public void onProviderEnabled(String provider) {
                    }

                    @Override
                    public void onProviderDisabled(String provider) {

                    }
                });

    }

    private void update(Location location) {
        tv_title.setText(location.getLatitude() +  "==="+location.getLongitude());
    }
}
